package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.page.PageData;
import io.renren.common.page.PageUtils;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.sys.dao.SysDictDataDao;
import io.renren.modules.sys.dto.SysDictDataDTO;
import io.renren.modules.sys.dto.SysDictDataQueryDto;
import io.renren.modules.sys.entity.SysDictDataEntity;
import io.renren.modules.sys.service.SysDictDataService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 字典类型
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataDao, SysDictDataEntity> implements SysDictDataService {

    @Override
    public PageData<SysDictDataDTO> page(SysDictDataQueryDto queryDto) {
        Page<SysDictDataDTO> page = new Page<>(queryDto.getPage(), queryDto.getLimit());
        PageUtils.setOrder(page, queryDto.getOrderField(), queryDto.getOrder());
        Page<SysDictDataDTO> pageVo = baseMapper.page(page, queryDto);
        return PageUtils.getPageData(pageVo.getRecords(), pageVo.getTotal(), SysDictDataDTO.class);
    }

    @Override
    public SysDictDataDTO get(Long id) {
        SysDictDataEntity entity = baseMapper.selectById(id);

        return ConvertUtils.sourceToTarget(entity, SysDictDataDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysDictDataDTO dto) {
        SysDictDataEntity entity = ConvertUtils.sourceToTarget(dto, SysDictDataEntity.class);

        this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysDictDataDTO dto) {
        SysDictDataEntity entity = ConvertUtils.sourceToTarget(dto, SysDictDataEntity.class);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long[] ids) {
        //删除
        this.removeBatchByIds(Arrays.asList(ids));
    }

}